package com.czy.moudle.exception.interceptor;

import com.czy.moudle.exception.domain.BaseExceptation;
import com.czy.moudle.exception.domain.enums.Type;
import com.czy.moudle.exception.service.BaseExceptionService;
import com.czy.moudle.exception.service.WebExceptionProcess;
import com.czy.moudle.exception.service.DBExceptionProcess;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/***
 * @author 陈致远
 * @date 2016/8/12
 */
public class MySimpleMappingExceptionResolver extends SimpleMappingExceptionResolver {
    private static Logger LOG = LoggerFactory.getLogger(MySimpleMappingExceptionResolver.class);

    @Resource(name = "baseExceptionServiceImpl")
    private BaseExceptionService exceptionService;

    @Resource(name = "webExceptionProcess")
    private WebExceptionProcess webExceptionProcess;

    @Resource(name = "dbExceptionProcess")
    private DBExceptionProcess dbExceptionProcess;

    @Override
    protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        BaseExceptation be = exceptionService.tarceException(ex);

        String viewName = this.determineViewName(ex, request);
        if (viewName != null) {

            if (be.getType().getName().equals(Type.web.getName())) {
                //web异常处理
                return webExceptionProcess.process(request, response, be);
            } else if (be.getType().getName().equals(Type.database.getName())) {
                // database存入数据库
            }
        }

        //统一打印日志信息
        LOG.info(be.toString());

        return null;
    }

    /*** Web异常处理
     * 1. Ajax请求响应异常信息
     * 2. 其他请求返回错误页面，展示异常信息
     */




}
